//创建 好单网数据
import AppConfig from '../config/appConfig';
import ShopItem from './shopItem';
import axiosUtil from "../utils/axiosUtil";
import AppStore from "../store/appStore";
import {IsNullObject} from "../utils/utils";

export function CreateItemBase(
  Id,
  Title,
  ShortTitle,
  Desc,
  Price,
  EndPrice,
  SaleCount,
  Pic,
  CouponUrl,
  CouponMoney,
  Tkrates,
  CouponStartTime,
  CouponEndTime,
  Fqcat,
  Couponexplain,
  ShopName
) {
  const item = new ShopItem();
  item.Id = Id;
  item.Title = Title;
  item.ShortTitle = ShortTitle;
  item.Desc = Desc;
  item.Price = Price;
  item.EndPrice = EndPrice;
  item.SaleCount = SaleCount;
  item.Pic = Pic;
  item.CouponUrl = CouponUrl;
  item.CouponMoney = CouponMoney;
  item.Tkrates = Tkrates;
  item.CouponStartTime = CouponStartTime;
  item.CouponEndTime = CouponEndTime;
  item.Fqcat = Fqcat;
  item.Couponexplain = Couponexplain;
  item.ShopName = ShopName;

  // if (CouponMoney >100){
  //   console.log(item)
  // }

  return item;
}

//创建 好单网数据
export function CreateHaoDanwangList(data) {

  const lists = [];
  data.map((item, key) => {
    lists.push(CreateHaoDanWangItem(item));
  });
  return lists;
}
export function CreateHaoDanWangItem(data) {
  const item = CreateItemBase(
    data.itemid,
    data.itemtitle,
    data.itemshorttitle,
    data.itemdesc,
    data.itemprice,
    data.itemendprice,
    data.itemsale,
    data.itempic,
    data.couponurl,
    data.couponmoney,
    data.tkrates,
    data.couponstarttime,
    data.couponendtime,
    data.fqcat,
    data.couponexplain,
    data.shopname,
  );
  //轮播图
  item.goods_gallery_urls.push(data.itempic);

  item.General_index = data.general_index  //好单指数

  item.Activityid = data.activityid

  //视频地址
  item.VideoUrl = data.videoid;
  item.TypeFrom = AppConfig.TaoBao;
  if (data.videoid > 0) {
    var url = 'http://cloud.video.taobao.com/play/u/1/p/1/e/6/t/1/+{0}+.mp4';
    item.VideoUrl = url.format(data.videoid);
  }

  if (IsNullObject( data.frist_order) === false && data.frist_order > 0){
    item.frist_order = data.frist_order

    var temp = item.EndPrice -  item.frist_order

    if (temp <=0){
      // console.log(item.EndPrice)
      // console.log(item.frist_order)
      // console.log(data)
      // console.log(item)
      item.EndPrice = 0
    }else {
      item.EndPrice =temp.toFixed(2);
    }

  }

  return item;
}

//创建拼多多数据
export function CreatePingDuoDuoList(data) {
  // console.log(data);
  const lists = [];
  data.map((item, key) => {
    lists.push(CreatePingDuoDuoItem(item));
  });
  return lists;
}
export function CreatePingDuoDuoItem(data) {
  const EndPrice = (data.min_group_price - data.coupon_discount) / 100;

  const item = CreateItemBase(
    data.goods_id, //宝贝ID
    data.goods_name, //宝贝标题
    '', //宝贝短标题
    data.goods_desc, //宝贝描述
    data.min_group_price / 100,
    EndPrice,
    data.sales_tip, //宝贝月销量
    data.goods_image_url, //主图原始图像
    '',
    data.coupon_discount / 100, //优惠券金额分
    data.promotion_rate, //佣金比例
    data.coupon_start_time, //优惠券生效时间，UNIX时间戳
    data.coupon_end_time, //优惠券失效时间，UNIX时间戳
    data.cat_ids, //商品类目id
    '',
    data.mall_name,
  );

  if (data.goods_gallery_urls === null)
    item.goods_gallery_urls.push(data.goods_image_url)
  else
    item.goods_gallery_urls = data.goods_gallery_urls; //商品轮播图

  item.goods_thumbnail_url = data.goods_thumbnail_url; //商品缩略图
  item.Min_normal_price = data.min_normal_price;
  item.Min_group_price = data.min_group_price;


  item.Desc_txt = data.desc_txt //描述分
  item.Lgst_txt = data.lgst_txt //物流分
  item.Serv_txt =  data.serv_txt //服务分

  item.TypeFrom = AppConfig.PingDuoDuo;

  item.Category_name = data.category_name
  return item;
}

//创建京东数据
export function CreateJinDongXiangList(data) {
  // console.log(data);
  const lists = [];
  data.map((item, key) => {
    lists.push(CreateJinDongXiangItem(item));
  });
  return lists;
}

export function CreateJinDongXiangItem(data) {
  // console.log(data)
  const item = CreateItemBase(
    data.jdSkuId==null?data.promotionId :data.jdSkuId,
    data.itemName,
    "",
    null,
    data.referPrice,
    data.salePrice,
    data.buyCount,
    data.itemMainPicUrl,
    data.couponUrl,
    data.coupon,
    null,
    null,
    null,
    null,
    null,
  );

  item.goods_gallery_urls.push(data.itemMainPicUrl)//商品轮播图

  item.TypeFrom = AppConfig.JinDong;

  item.item_url = data.materialId

  return item;
}

export  function getLink(item) {
  if (item.TypeFrom === AppConfig.TaoBao)
    return item.CouponUrl
  if (item.TypeFrom === AppConfig.JinDong)
    return item.clickURL
  if (item.TypeFrom === AppConfig.PingDuoDuo)
    return item.short_url
  return item.CouponUrl
}
